const ResolveRecordModel = require('../db/models/resolve_record');
//修改

class ResolveRecordServer { //修改
  //修改
  async addResolveRecord (data) {
    console.log(data);
    const problemId = data.problemId,
          userId = data.userId,
          submit_result = data.submit_result;
    //查询
    const result = await ResolveRecordModel.findOne({ where: { problemId, userId } });

    if(submit_result == 0 && result) {
      return null;
    }

    //判断 check
    if (result) { 
      //更新 update
      return await ResolveRecordModel.update(data, {where: { problemId, userId }})
    }else {
      //创建 create
      return await ResolveRecordModel.create(data);
    }
  }

  //找题目结果
  async findResolveRecordById(problemId, userId){
    return await ResolveRecordModel.findOne({
      where: {
        problemId,
        userId
      }
    })
  }

  //查 用户做题记录
  async findUserRecordsByUserId (userId) {
    return await ResolveRecordModel.findAll({
      where: {
        userId
      }
    })
  }

  async findRecordByIdGroup (pidArr, userId) {
    return await ResolveRecordModel.findAll({
      where: {
        pid: pidArr,
        userId
      }
    })
  }

}
//修改
module.exports = new ResolveRecordServer();